"""
Case Type   : mot
Case Name   : 打开enable_mot_server参数，使用copy to mot表，合理报错
Create At   : 2025/4/9
Owner       : @li-xin12345
Description : (编译带mot，不编译ENABLE_X86_RDTSCP，对应转测包)
    1、设置enable_incremental_checkpoint=off，enable_mot_server=on
    2、创建mot表
    3、执行COPY TO命令，构造数据文件
    4、清理环境
Expect      :
    1、成功
    2、成功
    3、合理报错
    4、成功
History     :
"""

import os
import unittest

from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class MotTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_db_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.tb_name = 't_guc_mot_0044'
        self.view_name = 'v_guc_mot_0044'
        self.copy_file = 'f_guc_mot_0044'
        self.param_names = ['enable_incremental_checkpoint',
                            'enable_mot_server']
        self.default_values = [self.common.show_param(i) for i in
                               self.param_names]
        self.copy_path = os.path.join(os.path.dirname(
            macro.DB_INSTANCE_PATH), self.copy_file)

    def test_mot(self):
        text = '-----step1:设置enable_incremental_checkpoint=off，' \
               'enable_mot_server=on;expect:成功-----'
        self.log.info(text)
        res = self.pri_db_sh.execute_gsguc('set',
                                        self.constant.GSGUC_SUCCESS_MSG,
                                        f'{self.param_names[0]}=off')
        self.log.info(res)
        self.assertTrue(res, "执行gs_guc set失败，预期成功但未找到标志信息")
        res = self.pri_db_sh.execute_gsguc('set',
                                        self.constant.GSGUC_SUCCESS_MSG,
                                        f'{self.param_names[1]}=on')
        self.log.info(res)
        self.assertTrue(res, "执行gs_guc set失败，预期成功但未找到标志信息")
        res = self.pri_db_sh.restart_db_cluster()
        self.log.info(res)
        self.assertTrue(res, '执行失败：' + text)
        status = self.pri_db_sh.get_db_cluster_status()
        self.assertTrue("Normal" or "Degrade" in status)
        res = [self.common.show_param(i) for i in self.param_names]
        self.log.info(res)
        self.assertEqual(['off', 'on'], res, '执行失败：' + text)

        text = '-----step2:创建mot表;expect:成功-----'
        self.log.info(text)
        sql = f'''drop foreign table if exists {self.tb_name};
        create foreign table {self.tb_name} (
            id int primary key,
            name varchar(50) not null,
            age int
        );
        insert into {self.tb_name} (id, name, age) 
        values (1, 'alice', 25), (2, 'bob', 30);
        '''
        sql_res = self.pri_db_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, sql_res,
                      '执行失败：' + text)
        self.assertIn(self.constant.CREATE_FOREIGN_SUCCESS_MSG, sql_res,
                      '执行失败：' + text)
        self.assertIn('INSERT 0 2', sql_res, '执行失败：' + text)

        text = '-----step3:执行COPY TO命令，构造数据文件-----'
        self.log.info(text)
        sql = f"copy {self.tb_name} to '{self.copy_path}.dat';"
        res = self.pri_db_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn('ERROR', res, '执行失败：' + text)
        self.assertIn('cannot copy from foreign table', res, '执行失败：' + text)

    def tearDown(self):
        text = '-----step4:清理环境; expect:成功-----'
        self.log.info(text)
        sql = f"drop foreign table if exists {self.tb_name} cascade;checkpoint;"
        drop_res = self.pri_db_sh.execut_db_sql(sql)
        self.log.info(drop_res)
        status = self.pri_db_sh.get_db_cluster_status(param='status')
        self.log.info(status)
        if status is True:
            self.log.info('--等待集群数据同步--')
            flag = self.pri_db_sh.check_data_consistency()
            self.assertTrue(flag, '执行失败：' + text)

        res = [self.common.show_param(i) for i in self.param_names]
        self.log.info(res)
        if res != self.default_values:
            for i, param_name in enumerate(self.param_names):
                res = self.pri_db_sh.execute_gsguc(
                    'set', self.constant.GSGUC_SUCCESS_MSG,
                    f'{param_name}={self.default_values[i]}')
                self.log.info(res)
            restart_res = self.pri_db_sh.restart_db_cluster()
            self.log.info(restart_res)
            status = self.pri_db_sh.get_db_cluster_status()
            self.log.info(status)
            show_param = [self.common.show_param(i) for i in self.param_names]
            self.log.info(show_param)
            self.assertEqual(self.default_values, show_param,
                             '执行失败：' + text)

        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, drop_res,
                      '执行失败：' + text)
        self.assertIn(self.constant.CHECKPOINT_SUCCESS_MSG, drop_res,
                      '执行失败：' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')